package cn.zd.erp.auth.user.service.ebo;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.zd.erp.auth.role.vo.Trole;
import cn.zd.erp.auth.user.dao.dao.UserDao;
import cn.zd.erp.auth.user.service.ebi.UserEbi;
import cn.zd.erp.auth.user.vo.Tuser;
import cn.zd.erp.util.base.PageFilter;
import cn.zd.erp.util.base.PageResults;
import cn.zd.erp.util.base.Tree;
import cn.zd.erp.util.base.User;

@Transactional
@Service
public class UserEbo implements UserEbi {
	@Autowired
	private UserDao userDao;

	public void save(Tuser t) {
		userDao.saveOrUpdate(t);
	}

	public void update(Tuser t) {

	}

	public void delete(Tuser t) {
		userDao.delete(t);
	}

	public List<Tuser> getAll() {
		return null;
	}

	public Tuser get(Serializable uuid) {

		return userDao.load(uuid);
	}

	/**
	 * <HQL分页查询>
	 * 
	 * @param hql
	 *            HQL语句
	 * @param countHql
	 *            查询记录条数的HQL语句
	 * @param pageNo
	 *            下一页
	 * @param pageSize
	 *            一页总条数
	 * @param values
	 *            不定Object数组参数
	 * @return PageResults的封装类，里面包含了页码的信息以及查询的数据List集合
	 * 
	 *         private int page;// 当前页 private int rows;// 每页显示记录数 private
	 *         String sort;// 排序字段 private String order;// asc/desc
	 */
	public List<Tuser> dataGrid(Tuser t, PageFilter ph) {
		return null;
	}

	public Long count(Tuser t, int page, String sort, int rows, String order) {
		return null;
	}

	public PageResults<User> dataGridCount(Tuser t, int page, String sort,
			int rows, String order) {
		String hql;

		if (t == null) {
			hql = "select distinct t from Tuser t order by t." + sort + " "
					+ order;
		} else {
			hql = "select distinct t from Tuser t where organization_id="
					+ t.getOrganization().getId() + " order by t." + sort + " "
					+ order;
		}
		PageResults<Tuser> list = userDao.findPageByFetchedHql(hql, null, page,
				rows, null);
		List<User> u1 = new ArrayList<>();
		for (Tuser t1 : list.getResults()) {
			User u = new User();
			BeanUtils.copyProperties(t1, u);
			Set<Trole> roles = t1.getRoles();
			if ((roles != null) && !roles.isEmpty()) {
				String roleIds = "";
				String roleNames = "";
				boolean b = false;
				for (Trole tr : roles) {
					if (b) {
						roleIds += ",";
						roleNames += ",";
					} else {
						b = true;
					}
					roleIds += tr.getId();
					roleNames += tr.getName();
				}
				u.setRoleIds(roleIds);
				u.setRoleNames(roleNames);
			}
			if (t1.getOrganization() != null) {
				u.setOrganizationId(t1.getOrganization().getId());
				u.setOrganizationName(t1.getOrganization().getName());
			}
			u1.add(u);
		}
		PageResults<User> pageResults = new PageResults<>();
		pageResults.setResults(u1);
		pageResults.setTotalCount(list.getTotalCount());
		return pageResults;
	}

	public Tuser getByName(String name) {
		String hql = "from Tuser t where t.loginname=?";
		Tuser t = userDao.getByHQL(hql, new String[] { name });
		return t;
	}

	public List<Tree> getTree() {
		return null;
	}

	public String getChRole(Tuser user) {
		String roleIds=null;
		Set<Trole> roles = user.getRoles();
		if ((roles != null) && !roles.isEmpty()) {
			 roleIds = "";
			 String roleNames = "";
			boolean b = false;
			for (Trole tr : roles) {
				if (b) {
					roleIds += ",";
					roleNames += ",";
				} else {
					b = true;
				}
				roleIds += tr.getId();
				roleNames += tr.getName();
			}
		}
		return roleIds;
	}

}
